home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / vgamaze4.zip / PLOT3D.H < prev    next >
C/C++ Source or Header  |  1994-03-02  |  11KB  |  229 lines

  1. #ifndef PLOT3D_H
  2. #define PLOT3D_H
  3.  
  4. //      This is an abstract class.  Classes derived from this class may be used
  5. // to instantiate objects that will plot z=f(x,y) in three dimensions.  The
  6. // derived classes must supply the functions "aspect ratio", 
  7. // "display_initialize", "pset", "num_x_pixels", "num_y_pixels", and 
  8. // "write_outfile".
  9.  
  10. //      This class uses the template "varray" (one dimensional virtual array)
  11. // and the class "titillat" (to let the user know the code is running).
  12.  
  13. #include "varray.h"
  14.  
  15. #define NUM_COLORS        65
  16. // Color 65 is reserved for parts of the plot to be highlighted.  Colors
  17. // 0 through 64 represent shades of gray from black to white.  So that parts
  18. // of a plot may be omitted, "pset" should not plot colors greater than 65.
  19.  
  20. #define SOLUTION_GRAY     64
  21. //      This is the shade of gray to be displayed for highlighted areas.
  22. // (On color displays, a color may displayed instead of a shade of gray.)
  23.  
  24. #define LIGHTEST_GRAY     61
  25. //      This is the lightest shade of gray that will actually be displayed
  26. // (for other than highlighted areas).
  27.  
  28. #define DARKEST_GRAY       8
  29. //      This is the darkest shade of gray that will actually be displayed
  30. // (for parts that can be seen).
  31.  
  32. typedef struct
  33.           {
  34.             unsigned char base_z;    // '1' - normal, '2' -- highlight, or
  35.                                      // '3' -- don't display; external to plot.
  36.             unsigned char color;     // shade of gray for this quadrilateral.
  37.             float         x;         // coordinates of corner of this
  38.             float         y;         // quadrilateral.
  39.             float         z;
  40.             int           x_division_index;
  41.             int           y_division_index;
  42.           } prime_rec, *prime_rec_ptr;
  43. //     One of the many quadrilaterals composing the plot.
  44.  
  45. typedef struct
  46.           {
  47.             double x;
  48.             double y;
  49.             double z;
  50.           } vertex_rec;
  51. //     A point or vector.
  52.  
  53. class plot3d
  54.   {
  55.     private:
  56.       void              adjust_perspective(void);
  57.                           // Make parallel lines running away from the viewer
  58.                           // appear to converge at the horizon.
  59.       unsigned char     color_max;
  60.                           // Maximum shade of gray for a quadrilateral
  61.                           // (before adjusted between DARKEST_GRAY and
  62.                           // LIGHTEST_GRAY).
  63.       unsigned char     color_min;
  64.                           // Minimum shade of gray for a quadrilateral
  65.                           // (before adjusted between DARKEST_GRAY and
  66.                           // LIGHTEST_GRAY).
  67.       void              evaluate_and_transform(double (*f)(double,double),
  68.                          double x_min,double x_max,double y_min,double y_max,
  69.                          int num_x_divisions,int num_y_divisions,
  70.                          double rotation,double tilt,
  71.                          int (*external_to_plot)(double,double),
  72.                          int (*red)(double,double));
  73.                           // Computes the vertices, etc. for each quadrilateral
  74.                           // composing the plot.
  75.       vertex_rec        light;
  76.                           // Vector pointing to the source of light. 
  77.       long              num_primes;
  78.                           // Number of quadrilaterals that compose the plot.
  79.       int               num_x_divisions;
  80.                           // Number of divisions of the x-axis used to define
  81.                           // the quadrilaterals composing the plot.
  82.       int               num_y_divisions;
  83.                           // Number of divisions of the y-axis used to define  
  84.                           // the quadrilaterals composing the plot.
  85.       int               plot_prepared;
  86.                           // TRUE if the last call to "prepare_plot" was
  87.                           // successful.
  88.       varray<prime_rec> *prime_array;
  89.                           // Virtual array of the quadrilaterals composing the
  90.                           // plot.
  91.       int               prime_array_allocated;
  92.                           // TRUE if "prime_array" was allocated.
  93.       void              quicksort(long,long);
  94.                           // Recursive quicksort.
  95.       void              rearrange(long,long,long *);
  96.                           // Called by "quicksort".
  97.       double            rotation;
  98.                           // Degrees the plot is rotated about the z-axis.
  99.       void              shade(void);
  100.                           // Computes the shade of gray for each quadrilateral
  101.                           // composing the plot.
  102.       void              sort_back_to_front();
  103.                           // The painter's algorithm is used; items farther
  104.                           // from the viewer are drawn earlier.
  105.       double            tilt;
  106.                           // Degrees the plot is tilted after it is rotated.
  107.       double            x_prime_max;
  108.       double            y_prime_max;
  109.       double            y_prime_min;
  110.       double            z_prime_max;
  111.       double            z_prime_min;
  112.     protected:
  113.       titillator *titillator_ptr;
  114.                     // Lets the user know the code is running.
  115.     public:
  116.       virtual double aspect_ratio(void) = 0;
  117. //      Derived classes must supply this.  The aspect ratio can be calculated
  118. // as (HEIGHT_OF_DISPLAY/WIDTH_OF_DISPLAY)/(NUM_Y_PIXELS/NUM_X_PIXELS) where
  119. //
  120. //           HEIGHT_OF_DISPLAY is the height of the display in centimeters, 
  121. //           WIDTH_OF_DISPLAY is the width of the display in centimeters,
  122. //           NUM_X_PIXELS is the width of the display in pixels, and
  123. //           NUM_Y_PIXELS is the height of the display in pixels.
  124.  
  125.       virtual int    display_initialized(void) = 0;
  126. //     Derived classes must supply this; it is called by "prepare_plot".  It
  127. // does whatever is necessary to initialize the display. 
  128.  
  129.                      plot3d(void);
  130.  
  131.       virtual        ~plot3d(void);
  132.  
  133.       virtual void   pset(int x,int y,int color_num) = 0;
  134. //     Derived classes must supply this; it is called by "plot".  It sets the
  135. // pixel at column "x" and row "y" to represent the shade of gray represented by
  136. // "color_num".
  137.  
  138.       virtual int    num_x_pixels(void) = 0;
  139. //     Derived classes must supply this; it is called by "plot".  It returns the
  140. // number of columns in the display.  That is, it returns the width of the
  141. // display in pixels.
  142.  
  143.       virtual int    num_y_pixels(void) = 0;
  144. //     Derived classes must supply this; it is called by "plot".  It returns the
  145. // number of rows in the display.  That is, it returns the height of the display
  146. // in pixels.
  147.  
  148.               int    plot(char *file_name,int show_red,
  149.                       int titillate=-1,double bias=0.5);
  150. //     This function returns TRUE if and only if it is successful in generating
  151. // the 3D plot.  It calls "aspect_ratio", "pset", and "write_outfile".  Its
  152. // parameters are as follow:
  153. //
  154. //           file_name -- the name of the file to which the plot is to be 
  155. //      written.  For plots on a cathode ray tube, this will usually be "".     
  156. //
  157. //           show_red -- highlight quadrilaterals having each vertex flagged
  158. //      to be highlighted.  Since only those quadrilaterals are redrawn,
  159. //      "plot" should not be called with "show_red" set to TRUE until after 
  160. //      it has been called with "show_red" set to FALSE.
  161. //
  162. //           titillate -- TRUE if the user is to be kept amused while the
  163. //      plot is being generated; FALSE otherwise.  In general, "titillate"
  164. //      should be TRUE for printers and FALSE for cathode ray tubes.
  165. //
  166. //           bias -- a positive number used to adjust the contrast.
  167. //
  168. // "prepare_plot" must be called before "plot", after which "plot" may be called
  169. // as many times as desired.
  170.  
  171.               int    prepare_plot(double (*f)(double,double),double x_min,
  172.                       double x_max,double y_min,double y_max,
  173.                       int (*external_to_plot)(double,dou